package com.yx.front.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yangx
 */
@Configuration
@Slf4j
public class RedisConfig {

    @Value("${redis.ids.host}")
    private String host;

    @Value("${redis.ids.port}")
    private Integer port;

    @Value("${redis.ids.password}")
    private String password;

    @Value("${redis.ids.index}")
    private Integer index;

    @Value("${redis.ids.timeout}")
    private int timeout;

    @Value("${redis.ids.pool.max-total}")
    private int maxTotal;

    @Primary
    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {


        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setHostName(this.host);
        factory.setPort(this.port);
        factory.setDatabase(this.index);
        factory.setTimeout(this.timeout);
        factory.setUsePool(true);
        factory.setPassword(this.password);

        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(this.maxTotal);
        factory.setPoolConfig(jedisPoolConfig);

        return factory;

    }


    @Primary
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) throws UnknownHostException {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory);
        GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
        // 设置key和value的序列化规则
        template.setKeySerializer(new GenericToStringSerializer<>(Object.class));
        template.setValueSerializer(genericFastJsonRedisSerializer);
        // 设置hashKey和hashValue的序列化规则
        template.setHashKeySerializer(new GenericToStringSerializer<>(Object.class));
        template.setHashValueSerializer(genericFastJsonRedisSerializer);

        template.afterPropertiesSet();
        return template;


    }

    @Bean
    public RedisCacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
        redisCacheManager.setUsePrefix(true);
        //默认缓存30分钟
        redisCacheManager.setDefaultExpiration(60 * 60 * 24);
        Map<String, Long> cacheTimes = new HashMap<>();
        redisCacheManager.setExpires(cacheTimes);
        return redisCacheManager;
    }
}